﻿using System;
using Enums;
using XF.ExControls;
using XF.Common;
using System.Windows.Forms;

namespace KYBM
{
    public partial class FrmDialogCustomerList : XFFormEx
    {
        XF.BLL.Base_Customer bll = new XF.BLL.Base_Customer();
        public XF.Model.Base_Customer Customer { get; set; } = new XF.Model.Base_Customer();

        public FrmDialogCustomerList()
        {
            InitializeComponent();
        }

        private void tsBtnSure_Click(object sender, EventArgs e)
        {
            xfDataGridView1.EndEdit();
            if(xfDataGridView1.SelectedRows.Count == 1)
            {
                Customer.Id = zDataConverter.ToInt(xfDataGridView1.SelectedRows[0].Cells[ColId.Name].Value);
                Customer.CustomerCode = zDataConverter.ToString(xfDataGridView1.SelectedRows[0].Cells[ColCode.Name].Value);
                Customer.CustomerName = zDataConverter.ToString(xfDataGridView1.SelectedRows[0].Cells[ColName.Name].Value);
                Customer.PayType = zDataConverter.ToInt(xfDataGridView1.SelectedRows[0].Cells[ColPayType.Name].Value);
                Customer.Remark = zDataConverter.ToString(xfDataGridView1.SelectedRows[0].Cells[ColRemark.Name].Value);
                Customer.CheckOutDate = DateTime.Parse(zDataConverter.ToString(xfDataGridView1.SelectedRows[0].Cells[ColCheckOutDate.Name].Value));
                Customer.Arrears = zDataConverter.ToDecimal(xfDataGridView1.SelectedRows[0].Cells[ColArrears.Name].Value);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void FrmDialogCustomerListt_Load(object sender, EventArgs e)
        {
            xfDataGridView1.AutoGenerateColumns = false;
            ColPayType.DisplayMember = "Text";
            ColPayType.ValueMember = "Value";
            ColPayType.DataSource = GenerateData.GetPayType(false);
            BindData();
        }

        private void BindData()
        {
            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(tsTbCustomer.Text.Trim()))
            {
                strWhere += " (CustomerCode like '%" + tsTbCustomer.Text.Trim() + "%' or CustomerName like '%" + tsTbCustomer.Text.Trim() + "%')";
            }
            xfDataGridView1.DataSource = bll.GetList(strWhere).Tables[0];
        }

        private void tsBtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void FrmDialogCustomerList_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    tsBtnSure_Click(this, EventArgs.Empty);
                    break;
                case Keys.Escape:
                    tsBtnCancel_Click(this, EventArgs.Empty);
                    break;
            }
        }
    }
}
